package com.sdkj.controller;

import com.sdkj.pojo.Checkgroup;
import com.sdkj.pojo.Setmeal;
import com.sdkj.service.SetmealService;
import com.sdkj.util.MessageConstant;
import com.sdkj.util.PageResult;
import com.sdkj.util.QueryPageBean;
import com.sdkj.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;
    @RequestMapping("/uploadFile")
    public Result uploadFile(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取上传图片的名称
        String filename = imgFile.getOriginalFilename();
        //最后一次出现点的坐标
        int lastIndexOf = filename.lastIndexOf(".");
        filename.substring(lastIndexOf - 1);
        String suffix = filename.substring(lastIndexOf - 1);
        //前缀 44jiui4k7645l23lkjkj765l432j
        String prefix = UUID.randomUUID().toString();
        String newFileName = prefix + suffix; //44jiui4k7645l23lkjkj765l432j.jpg
        //上传文件的路径
        String path = "F:/j_demo/photos/";
        File file = new File(path + newFileName);
        try {
            //图片上传
            imgFile.transferTo(file);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, newFileName);
    }
    //套餐添加
    @RequestMapping("/addSetmealInfo")
    public Result addSetmealInfo(Integer[] ids,@RequestBody Setmeal setmeal){
        boolean flag = setmealService.addSetmealInfo(ids,setmeal);
        if (flag){
            return  new Result(flag,MessageConstant.ADD_SETMEAL_SUCCESS);
        }else{
            return  new Result(flag,MessageConstant.ADD_SETMEAL_FAIL);}
    }

    //分页展示套餐信息
    @RequestMapping("/findPageInfo")
    public PageResult findPageInfo(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPageInfo(queryPageBean);
        return pageResult;
    }
    //删除套餐
    @RequestMapping("/deleteInfoById")
    public Result deleteInfoById(Integer id) {
        boolean flag = setmealService.deleteInfoById(id);
        if (flag) {
            return new Result(flag, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } else {
            return new Result(flag, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
    //根据套餐id 获取该套餐对应的所有检查组id值
    @RequestMapping("/getGroupIds")
    public Result getGroupIds(Integer id) {
        Result result = setmealService.getGroupIds(id);
        return result;
    }
    @RequestMapping("/updateSetmealInfo")
    public Result updateSetmealInfo(Integer[] ids, @RequestBody Setmeal setmeal){
        boolean flag = setmealService.updateSetmealInfo(ids,setmeal);
        if (flag){
            return  new Result(flag,MessageConstant.ADD_SETMEAL_SUCCESS);
        }else{
            return  new Result(flag,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }


}
